package org.dllwh.nacos.sdk;


import cn.hutool.core.util.IdUtil;
import cn.hutool.setting.dialect.Props;
import cn.hutool.setting.dialect.PropsUtil;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
import com.alibaba.nacos.common.utils.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * <p>
 * Today the best performance as tomorrow the newest starter!
 *
 * @ClassName NacosServiceTemplate
 * @Description Nacos 服务管理 SDK
 * @Date 2025-08-25 14:33
 * @版本 V 1.0.1
 * @since JDK 17
 */
public class NacosServiceTemplate {
    private static final Props PROPS = PropsUtil.get("nacos.properties");

    /**
     * 获取当前客户端所在命名空间的服务列表
     *
     * @param pageNo    分页序号
     * @param pageSize  分页中每页的服务个数，最大值 500
     * @param groupName 分组名，缺省默认值 DEFAULT_GROUP
     */
    public static List<String> getServiceList(int pageNo, int pageSize, String groupName) throws NacosException {
        pageNo = Math.max(pageNo, 1);
        pageSize = Math.max(pageSize, 10);
        pageSize = Math.min(pageSize, 500);

        NamingService namingService = NamingFactory.createNamingService(PROPS);
        ListView<String> serverViewList = StringUtils.isBlank(groupName)
                ? namingService.getServicesOfServer(pageNo, pageSize)
                : namingService.getServicesOfServer(pageNo, pageSize, groupName);

        return serverViewList.getData();
    }

    /**
     * 获取某服务下的所有实例
     *
     * @param serviceName 服务名，必须
     * @param groupName   分组名，缺省默认值 DEFAULT_GROUP
     */
    public static List<Instance> getServiceInstanceList(String serviceName, String groupName) throws NacosException {
        NamingService namingService = NamingFactory.createNamingService(PROPS);
        return StringUtils.isBlank(groupName)
                ? namingService.getAllInstances(serviceName)
                : namingService.getAllInstances(serviceName, groupName);
    }

    /**
     * 根据条件获取过滤后的实例列表。
     *
     * @param serviceName 服务名
     * @param groupName   分组名，缺省默认值 DEFAULT_GROUP
     * @param healthy     是否健康
     */
    public static List<Instance> getServiceInstanceList(String serviceName, String groupName, boolean healthy) throws NacosException {
        NamingService namingService = NamingFactory.createNamingService(PROPS);
        return StringUtils.isBlank(groupName)
                ? namingService.selectInstances(serviceName, healthy)
                : namingService.selectInstances(serviceName, groupName, healthy);
    }

    /**
     * 根据负载均衡算法随机获取一个健康实例。
     *
     * @param serviceName 服务名
     * @param groupName   分组名，缺省默认值 DEFAULT_GROUP
     */
    public static Instance selectInstances(String serviceName, String groupName) throws NacosException {
        NamingService namingService = NamingFactory.createNamingService(PROPS);
        return StringUtils.isBlank(groupName)
                ? namingService.selectOneHealthyInstance(serviceName)
                : namingService.selectOneHealthyInstance(serviceName, groupName);
    }


    /**
     * 创建Nacos实例
     */
    public void createNacosInstance(String serviceName, String groupName, String ip, int port) throws NacosException {
        NamingService namingService = NamingFactory.createNamingService(PROPS);
        if (StringUtils.isBlank(groupName)) {
            namingService.registerInstance(serviceName, ip, port);
        } else {
            namingService.registerInstance(serviceName, groupName, ip, port);
        }
    }

    /**
     * 删除Nacos实例
     */
    public void deleteNacosInstance(String serviceName, String groupName, String ip, int port) throws NacosException {
        NamingService namingService = NamingFactory.createNamingService(PROPS);
        if (StringUtils.isBlank(groupName)) {
            namingService.deregisterInstance(serviceName, ip, port);
        } else {
            namingService.deregisterInstance(serviceName, groupName, ip, port);
        }
    }
}
